import {
  inspectionDailyReport
} from '../../service/service'
Page({
  data: {
    pageSize: 10,
    pageNum: 1,
    reports: [],
    total: 0,
    currentTotal: 0,
    showPhotoModal: false,
    modalPhotos: [],
    currentModalPhotoIndex: 0
  },
  onLoad() {
    this.getDailyReports()
  },
  onPullDownRefresh() {
    console.log('下拉刷新')
  },
  async getDailyReports() {
    try {
      const params = {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      }
      const res = await inspectionDailyReport(params)
      const {
        total,
        rows
      } = res
      const newReports = rows.map(item => {
        const inspectionPhoto = typeof item.inspectionPhoto === 'string' ?
          JSON.parse(item.inspectionPhoto) :
          item.inspectionPhoto;
        const attachment = typeof item.attachment === 'string' ?
          JSON.parse(item.attachment) :
          item.attachment;
        const changeReport = {
          ...item,
          inspectionPhoto,
          attachment
        }
        return changeReport
      })
      this.setData({
        reports: newReports,
        total: total
      })
      console.log(this.data.reports)
    } catch (e) {
      my.showToast({
        content: e,
        type: "fail",
        duration: 2000
      })
    }
  },
  // 图片轮播事件
  onPhotoSwiper(e) {
    const {
      current
    } = e.detail;
    const {
      index
    } = e.currentTarget.dataset;

    this.setData({
      [`reports[${index}].currentPhotoIndex`]: current
    });
  },

  onPhotoTap(e) {
    const {
      url,
      index,
      photoIndex
    } = e.currentTarget.dataset;
    const report = this.data.reports[index];

    this.setData({
      showPhotoModal: true,
      modalPhotos: report.inspectionPhotos,
      currentModalPhotoIndex: photoIndex || 0
    });
  },

  // 模态框图片切换
  onModalPhotoChange(e) {
    const {
      current
    } = e.detail;
    this.setData({
      currentModalPhotoIndex: current
    });
  },

  // 关闭图片预览模态框
  closePhotoModal() {
    this.setData({
      showPhotoModal: false,
      modalPhotos: [],
      currentModalPhotoIndex: 0
    });
  },
});